package model;

import java.util.Map;
import java.util.TreeMap;

public final class ClassVersion implements Comparable<ClassVersion> {
	
	public static final String CURRENT_CLASS_VERSION = System.getProperty("java.class.version");
	
	private static final Map<String, String> versionMap = new TreeMap<String, String>();
	
	static {
		versionMap.put("45.3", "1.1");
		versionMap.put("46.0", "1.2");
		versionMap.put("47.0", "1.3");
		versionMap.put("48.0", "1.4");
		versionMap.put("49.0", "5 (1.5)");
		versionMap.put("50.0", "6 (1.6)");
		versionMap.put("51.0", "7 (1.7)");
		versionMap.put("52.0", "8 (1.8)");
	}
	
	private int minor;
	private int major;
	private String jdk;

	public ClassVersion(int minor, int major) {
		this.minor = minor;
		this.major = major;
		this.jdk = getJDKVersionOfClassVersion(major, minor);
	}

	public int getMajor() {
		return this.major;
	}

	public int getMinor() {
		return this.minor;
	}

	public String getJdk() {
		return jdk;
	}
	
	@Override
	public String toString() {
		return "ClassVersion [minor=" + minor + ", major=" + major + ", jdk="
				+ jdk + "]";
	}

	public static String getJDKVersionOfClassVersion(String classVersion) {
		return versionMap.get(classVersion);		
	}

	public static String getJDKVersionOfClassVersion(int major, int minor) {
		return getJDKVersionOfClassVersion(major+"."+minor);
	}

	@Override
	public int compareTo(ClassVersion o) {
		if (o != null) {
			if (o.major > this.major) {
				return 1;
			}
			else if (o.major < this.major) {
				return -1;
			}
			else {
				if (o.minor > this.minor) {
					return 1;
				}
				else if (o.minor < this.minor) {
					return -1;
				}
				else {
					return 0;
				}
			}
		}
		
		return 0;
	}
	
}